package BookManagement.dao;

import BookManagement.domain.Book;
import BookManagement.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public interface UserDao {
    @Insert("insert into user values(#{userName},#{password},#{identity},#{name},#{sex},#{birth},#{photo},#{balance},#{credit},#{borrowNum},#{purchaseNum},#{point},#{signature})")
    public int addUser(User user);

    @Select("select * from user where username=#{userName}")
    public User findUserByUsername(String userName);

    @Delete("delete from user where username=#{userName}")
    public int deleteUserByUsername(String userName);

    @Update("<script>"+
            "update user"+"<set>"+
            "<if test='password!=null'>"+"password=#{password},"+"</if>"+
            "<if test='identity!=null'>"+"identity=#{identity},"+"</if>"+
            "<if test='name!=null'>"+"name=#{name},"+"</if>"+
            "<if test='sex!=null'>"+"sex=#{sex},"+"</if>"+
            "<if test='birth!=null'>"+"birth=#{birth},"+"</if>"+
            "<if test='photo!=null'>"+"photo=#{photo},"+"</if>"+
            "<if test='signature!=null'>"+"signature=#{signature},"+"</if>"+
            "<if test='balance!=null'>"+"balance=#{balance},"+"</if>"+
            "<if test='credit!=null'>"+"credit=#{credit},"+"</if>"+
            "<if test='borrowNum!=null'>"+"borrowNum=#{borrowNum},"+"</if>"+
            "<if test='purchaseNum!=null'>"+"purchaseNum=#{purchaseNum},"+"</if>"+
            "<if test='point!=null'>"+"point=#{point},"+"</if>"+
            "</set>"+"where username=#{userName}"+"</script>"
    )
    public int updateUser(User user);

    @Update("update user set borrowNum=borrowNum+1 where username=#{username}")
    public int updateBorrowNum(String username);

    @Update("update user set borrowNum=borrowNum-1 where username=#{username}")
    public int updateBorrowNum2(String username);

    @Select("select count(*) from user where identity='user' ")
    public int usercount();

    @Select("<script>"+"select * from user where identity='user' "+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<User> findAllUsers(HashMap<String,Object> map);

}
